//
//  XMWebViewController.m
//  XiaoMiao
//
//  Created by 23 on 2018/2/6.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMWebViewController.h"

@interface XMWebViewController () <UIWebViewDelegate>

/**webView*/
@property(nonatomic,weak) UIWebView *webView;

@end

@implementation XMWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

#pragma mark - UI
- (void)setupUI
{
    NSString *typeStr = @"";
    switch (self.type) {
        case XMWebViewTypeAbout:
        {
            self.title = @"关于我们";
            typeStr = @"abountM";
        }
            break;
        case XMWebViewTypeHelp:
        {
            self.title = @"帮助中心";
            typeStr = @"helpCenter";
        }
            break;
        case XMWebViewTypeContact:
        {
            self.title = @"联系客服";
            typeStr = @"contact";
        }
            break;
            
        default:
            break;
    }
    
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    [self.view addSubview:webView];
    self.webView = webView;
    
    NSString *url = [NSString stringWithFormat:@"%@/api/biz/gl/v1/glConfigs/get.htm",kApiServer];
//    NSString *url = @"http://120.25.104.131:8091/photograph/api/biz/gl/v1/glConfigs/get.htm";
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"key"] = typeStr;
    
    WEAKSELF
    [KLProgressHUD showWaitingHUDWithSuperView:self.view];
    [AppNetwork getWithUrl:url refreshCache:NO params:parameters success:^(id response) {
        [KLProgressHUD hideHUDForView:self.view animated:YES];
        NSDictionary *glConfigViews = response[@"glConfigViews"];
        NSString *content = glConfigViews[@"content"];
        NSString *name = glConfigViews[@"name"];
        
        weakSelf.title = name;
        [weakSelf.webView loadHTMLString:content baseURL:nil];
        
    } fail:^(AppError *error) {
        [KLProgressHUD hideHUDForView:self.view animated:YES];
        [KLProgressHUD showErrorHUDWithError:error];
    }];
    
    
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    self.webView.frame = self.view.bounds;
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [KLProgressHUD showWaitingHUDWithSuperView:self.view];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [KLProgressHUD hideHUDForView:self.view animated:YES];
}

@end
